<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Using generic views for your serializable models &mdash; django-serializable v0.1 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '0.1',
        COLLAPSE_MODINDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <link rel="top" title="django-serializable v0.1 documentation" href="../index.html" />
    <link rel="prev" title="Writing serializable models" href="models.html" /> 
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             accesskey="M">modules</a> |</li>
        <li class="right" >
          <a href="models.html" title="Writing serializable models"
             accesskey="P">previous</a> |</li>
        <li><a href="../index.html">django-serializable v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-serializable.views">
<h1>Using generic views for your serializable models<a class="headerlink" href="#module-serializable.views" title="Permalink to this headline">¶</a></h1>
<p>For each one of these views, the keyword argument <cite>model</cite> is mandatory.</p>
<p>For <a title="serializable.views.delete_serializable" class="reference internal" href="#serializable.views.delete_serializable"><tt class="xref docutils literal"><span class="pre">delete_serializable()</span></tt></a> and <a title="serializable.views.get_or_post_serializable" class="reference internal" href="#serializable.views.get_or_post_serializable"><tt class="xref docutils literal"><span class="pre">get_or_post_serializable()</span></tt></a>, it is also mandatory to provide a natural key by passing each pair <cite>(nk_field_name, value)</cite> as a keyword argument.</p>
<p>This can be done by mapping directly in the url. For example :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url</span><span class="p">(</span><span class="s">&#39;^someurl/(?&lt;nk_field_name_1&gt;\w+)/(?&lt;nk_field_name_2&gt;\w*/$&#39;</span><span class="p">,</span> <span class="n">get_or_post_serializable</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;model&#39;</span><span class="p">:</span> <span class="n">MyModel</span><span class="p">})</span>
</pre></div>
</div>
<p>In this case, any request issued for example to the url <cite>&#8216;/someurl/jj/cale/&#8217;</cite>, will result in the keywords arguments <cite>(nk_field_name_1=&#8217;jj&#8217;, nk_field_name_2=&#8217;cale&#8217;)</cite> passed to the view, and therefore the object accessed is identified by the natural key <cite>(&#8216;jj&#8217;, &#8216;cale&#8217;)</cite>.</p>
<p>This can also be done by providing explicitely these natural key values as arguments :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url</span><span class="p">(</span><span class="s">&#39;^someurl/(?&lt;nk_field_name_2&gt;\d*/$&#39;</span><span class="p">,</span> <span class="n">get_or_post_serializable</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="s">&#39;nk_field_name_1&#39;</span><span class="p">:</span> <span class="s">&#39;jj&#39;</span><span class="p">})</span>
</pre></div>
</div>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In the following example <cite>(&#8216;nk_field_name_1&#8217;, &#8216;nk_field_name_2&#8217;)</cite> is the natural key blueprint for <cite>model</cite>. See <tt class="xref docutils literal"><span class="pre">serializable.models.Serializable.nk_field_names</span></tt> for more info.</p>
</div>
<dl class="function">
<dt id="serializable.views.delete_serializable">
<tt class="descclassname">serializable.views.</tt><tt class="descname">delete_serializable</tt><big>(</big><em>request</em>, <em>model=None</em>, <em>login_required=False</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#serializable.views.delete_serializable" title="Permalink to this definition">¶</a></dt>
<dd><p>This view allows to delete a serializable object.</p>
<p>The object is fetched with its natural key, and deleted from the database.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameter:</th><td class="field-body"><em>model</em> &#8211; The model to manage. Must be a subclass of <a title="serializable.models.Serializable" class="reference external" href="models.html#serializable.models.Serializable"><tt class="xref docutils literal"><span class="pre">serializable.models.Serializable</span></tt></a></td>
</tr>
</tbody>
</table>
</dd></dl>

<dl class="function">
<dt id="serializable.views.get_or_post_serializable">
<tt class="descclassname">serializable.views.</tt><tt class="descname">get_or_post_serializable</tt><big>(</big><em>request</em>, <em>model=None</em>, <em>login_required=False</em>, <em>format='pickle'</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#serializable.views.get_or_post_serializable" title="Permalink to this definition">¶</a></dt>
<dd><p>This view allows to get, create or update a serializable object.</p>
<p>If the request issued is a POST, either the object with natural key passed as keyword arguments exist, then it is updated ; or it doesn&#8217;t exist, then it is created.</p>
<blockquote>
<ul class="simple">
<li>If creating or updating : the data POSTed in the <cite>serializable_data</cite> parameter of the request (i.e. <cite>request.POST[&#8216;serializable_data&#8217;]</cite>) will be decoded (according to <cite>&lt;format&gt;</cite>), then passed to the <tt class="xref docutils literal"><span class="pre">serializable.models.Serializable.deserialize()</span></tt> method as a python dictionnary to populate object fields.</li>
<li>If creating : the natural key&#8217;s <cite>(nk_field_name, value)</cite> pairs will be passed along with the decoded data in the python dictionnary.</li>
<li>In all cases : the object (created, updated or unchanged) is sent as an answer, encoded in <cite>&lt;format&gt;</cite>.</li>
</ul>
</blockquote>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><em>model</em> &#8211; The model to manage. Must be a subclass of <a title="serializable.models.Serializable" class="reference external" href="models.html#serializable.models.Serializable"><tt class="xref docutils literal"><span class="pre">serializable.models.Serializable</span></tt></a></li>
<li><em>format</em> &#8211; The format used to decode the request parameters, and to encode the answer.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Pretty much all the serialization/deserialization work is handled by the model. So the behaviour depends on the way you parametered them.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="serializable.views.get_serializable_list">
<tt class="descclassname">serializable.views.</tt><tt class="descname">get_serializable_list</tt><big>(</big><em>request</em>, <em>model=None</em>, <em>login_required=False</em>, <em>format='pickle'</em>, <em>filters=</em><span class="optional">[</span><span class="optional">]</span>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#serializable.views.get_serializable_list" title="Permalink to this definition">¶</a></dt>
<dd><p>This view allows to get a list of serializable objects.</p>
<p>You can filter the results that this method send. For example, you can declare the following in your urlconf :</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">url</span><span class="p">(</span><span class="s">&#39;^someurl/(?&lt;author__family__name&gt;\w+)/$&#39;</span><span class="p">,</span> <span class="n">kwargs</span><span class="o">=</span><span class="p">{</span><span class="n">filters</span><span class="p">:</span> <span class="p">[</span><span class="s">&#39;author__family__name&#39;</span><span class="p">],</span> <span class="s">&#39;model&#39;</span><span class="p">:</span> <span class="n">MyModel</span><span class="p">})</span>
</pre></div>
</div>
<p>If issuing a request to <cite>&#8216;/someurl/marshall/&#8217;</cite>, the results in the list will be only the elements of <cite>MyModel</cite> whose author&#8217;s family&#8217;s name is <cite>&#8216;marshall&#8217;</cite>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><em>model</em> &#8211; The model to manage. Must be a subclass of <a title="serializable.models.Serializable" class="reference external" href="models.html#serializable.models.Serializable"><tt class="xref docutils literal"><span class="pre">serializable.models.Serializable</span></tt></a></li>
<li><em>format</em> &#8211; The format used to encode the answer.</li>
<li><em>filters</em> &#8211; A list of attribute filters. Supported syntax is the same as <a class="reference external" href="http://docs.djangoproject.com/en/dev/ref/models/querysets/#id7">field lookup</a> syntax. The filters declared in this list should correspond to keyword arguments passed when the view is called.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Pretty much all the serialization work is handled by the model. So the behaviour depends on the way you parametered them.</p>
</div>
</dd></dl>

</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
            <h4>Previous topic</h4>
            <p class="topless"><a href="models.html"
                                  title="previous chapter">Writing serializable models</a></p>
            <h3>This Page</h3>
            <ul class="this-page-menu">
              <li><a href="../_sources/doc_pages/views.txt"
                     rel="nofollow">Show Source</a></li>
            </ul>
          <div id="searchbox" style="display: none">
            <h3>Quick search</h3>
              <form class="search" action="../search.html" method="get">
                <input type="text" name="q" size="18" />
                <input type="submit" value="Go" />
                <input type="hidden" name="check_keywords" value="yes" />
                <input type="hidden" name="area" value="default" />
              </form>
              <p class="searchtip" style="font-size: 90%">
              Enter search terms or a module, class or function name.
              </p>
          </div>
          <script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../modindex.html" title="Global Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="models.html" title="Writing serializable models"
             >previous</a> |</li>
        <li><a href="../index.html">django-serializable v0.1 documentation</a> &raquo;</li> 
      </ul>
    </div>
    <div class="footer">
      &copy; Copyright 2010, Sebastien Piquemal.
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 0.6.4.
    </div>
  </body>
</html>